//
//  WBEmoticonToolBar.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/23.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBEmoticonToolBar.h"
typedef enum : NSUInteger {
    EmoticonTypeRecent = 0,
    EmoticonTypeDefult,
    EmoticonTypeEmoji,
    EmoticonTypeLXH,
} EmoticonType;

@interface WBEmoticonToolBar ()

@property(nonatomic ,weak) UIButton *lastButton;

@end

@implementation WBEmoticonToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self setupUI];
    }
    return self;
}

-(void)setupUI {

    [self addBotton:@"最近" backgroundImageName:@"compose_emotion_table_left" index: 0];
    [self addBotton:@"默认" backgroundImageName:@"compose_emotion_table_mid" index: 1];
    [self addBotton:@"Emoji" backgroundImageName:@"compose_emotion_table_mid"index: 2];
    [self addBotton:@"浪小花" backgroundImageName:@"compose_emotion_table_right"
     index: 3];
    
    //默认自身为0 ，与botton 有冲突
    self.tag = 100;
    
}

-(void)addBotton:(NSString *)title backgroundImageName:(NSString *)imageName index:(NSInteger)index{

    UIButton *btn = [UIButton buttonWithType:0];
    
    btn.tag = index;
    
    //切图
    UIImage *normalImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",imageName]] stretchableImageWithLeftCapWidth:3 topCapHeight:18];
    UIImage *selImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] stretchableImageWithLeftCapWidth:3 topCapHeight:18];
    
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
    //uibutton 取消点击效果
    btn.adjustsImageWhenHighlighted = NO;
    
    [btn setBackgroundImage:normalImage   forState:UIControlStateNormal];
   
    [btn setBackgroundImage:selImage forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(chooseEmoticon:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addArrangedSubview:btn];
    //默认第一个选中
    if (index == EmoticonTypeRecent) {
        
        btn.selected = YES;
        self.lastButton = btn;
    }

}

-(void)chooseEmoticon:(UIButton *)btn{

    if (btn.selected) {
        return;
    }
    
    btn.selected = YES;
    self.lastButton.selected = NO;
    self.lastButton = btn;
    
    if (self.emoticonClickBlock != nil) {
        
        self.emoticonClickBlock(btn);
    }
}

-(void)setEmoticonTypeSelected:(NSIndexPath *)indexPath{

    UIButton *btn = [self viewWithTag:indexPath.section];
    
    if (btn.selected) {
        return;
    }
    
    btn.selected = YES;
    self.lastButton.selected = NO;
    self.lastButton = btn;

    
}

@end
